<?php
class PapiWS_ProducttemplaterendererController extends Ext_Controller_Action
{

	// image canvas
	private $canvas;

	private $draw;

	private $maskPlainColor = '#990000';
	private $maskAlpha = 0.3;
	private $viewportSizeMultiplier = 2;
	private $backgroundBorder = 5;

	public function maskAction( )
	{
		dl('imagick.so');

        $width  = $this->getRequest()->getParam('width');
        $height = $this->getRequest()->getParam('height');

		$widthPx  = $this->mmToPx( $width );
		$heightPx = $this->mmToPx( $height );

        $maskWidth  = $width * $this->viewportSizeMultiplier;
        $maskHeight = $height * $this->viewportSizeMultiplier;

        $top    = ($maskHeight - $height) / 2;
        $left   = ($maskWidth - $width) / 2;

		$maskWidthPx  = $this->mmToPx( $maskWidth );
		$maskHeightPx = $this->mmToPx( $maskHeight );

		$this->prepareCanvas($maskWidthPx, $maskHeightPx);

		$this->drawMask( $maskWidthPx, $maskHeightPx, $top, $left, $widthPx, $heightPx );
	
		$this->drawCanvas();

		$this->outputImage();
	}

	public function backgroundAction ( )
	{
		dl('imagick.so');

     	$width  = $this->getRequest()->getParam('width');
        $height = $this->getRequest()->getParam('height');

		$widthPx  = $this->mmToPx( $width );
		$heightPx = $this->mmToPx( $height );

        $maskWidth  = $width * $this->viewportSizeMultiplier;
        $maskHeight = $height * $this->viewportSizeMultiplier;

        $top    = ($maskHeight - $height) / 2;
        $left   = ($maskWidth - $width) / 2;

		$maskWidthPx  = $this->mmToPx( $maskWidth );
		$maskHeightPx = $this->mmToPx( $maskHeight );

		$this->prepareCanvas($maskWidthPx, $maskHeightPx);

		$this->drawBackground( $maskWidthPx, $maskHeightPx, $top, $left, $widthPx, $heightPx );
	
		$this->drawCanvas();

		$this->outputImage();

   	}

	public function maskpreviewAction ( )
	{
		dl('imagick.so');

        $width  = $this->getRequest()->getParam('width');
        $height = $this->getRequest()->getParam('height');

		$widthPx  = $this->mmToPx( $width );
		$heightPx = $this->mmToPx( $height );

        $maskWidth  = $width * $this->viewportSizeMultiplier;
        $maskHeight = $height * $this->viewportSizeMultiplier;

        $top    = ($maskHeight - $height) / 2;
        $left   = ($maskWidth - $width) / 2;

		$maskWidthPx  = $this->mmToPx( $maskWidth );
		$maskHeightPx = $this->mmToPx( $maskHeight );

		$this->prepareCanvas($maskWidthPx, $maskHeightPx);

		$this->drawMaskPreview( $maskWidthPx, $maskHeightPx, $top, $left, $widthPx, $heightPx );
	
		$this->drawCanvas();

		$this->outputImage();
	
	}

	// inicializa el canvas
	private function prepareCanvas ( $width, $height )
	{
		$this->canvas = new Imagick();
		$this->canvas->newImage( $width, $height, 'transparent' );
	}

	// dibuja la mask en el draw
	private function drawMask ( $maskWidth, $maskHeight, $boxTop, $boxLeft, $boxWidth, $boxHeight )
	{
		$this->draw = new ImagickDraw();
        $this->draw->setFillColor( $this->maskPlainColor );
        $this->draw->setFillAlpha( $this->maskAlpha );
        $this->draw->rectangle( 0, 0, $maskWidth, $maskHeight);
        $this->draw->setFillAlpha( 1 );
        $this->draw->roundRectangle( 
			$this->mmToPx($boxLeft), 
			$this->mmToPx($boxTop), 
			$this->mmToPx($boxLeft)+ $boxWidth, 
			$this->mmToPx($boxTop) + $boxHeight, 
			5, 5 );

	}

	// dibuja un maskPreview en el draw 
	private function drawMaskPreview ( $maskWidth, $maskHeight, $boxTop, $boxLeft, $boxWidth, $boxHeight )
	{
		$this->draw = new ImagickDraw();
        $this->draw->setFillColor( '#000000' );
        $this->draw->setFillAlpha( 0 );
        $this->draw->rectangle( 0, 0, $maskWidth, $maskHeight);
        $this->draw->setFillColor( $this->maskPlainColor );
        $this->draw->setFillAlpha( 1 );
        $this->draw->roundRectangle( 
			$this->mmToPx($boxLeft), 
			$this->mmToPx($boxTop), 
			$this->mmToPx($boxLeft)+ $boxWidth, 
			$this->mmToPx($boxTop) + $boxHeight, 
			5, 5 );


	}

	// dibuja un Background en el draw
	private function drawBackground ( $maskWidth, $maskHeight, $boxTop, $boxLeft, $boxWidth, $boxHeight )
	{
		$this->draw = new ImagickDraw();
        $this->draw->setFillColor( '#FFFFFF' );
        $this->draw->setFillAlpha( 1 );
        $this->draw->rectangle( 0, 0, $maskWidth, $maskHeight);
        $this->draw->setFillColor( '#000000' );
        $this->draw->setFillAlpha( 1 );
        $this->draw->roundRectangle( 
			$this->mmToPx($boxLeft) - $this->backgroundBorder,
			$this->mmToPx($boxTop) - $this->backgroundBorder, 
			$this->mmToPx($boxLeft)+ $boxWidth + $this->backgroundBorder, 
			$this->mmToPx($boxTop) + $boxHeight + $this->backgroundBorder, 
			5, 5 );
	}

	// dibuja el draw en el canvas
	private function drawCanvas ( )
	{
        $this->canvas->drawImage( $this->draw );
	}

	// imprime la imagen generada
	private function outputImage ( )
	{
		header("Content-Type: image/png");
		$this->canvas->setImageFormat('png');
		echo $this->canvas;
		die();

	}

	public function getCanvas()
	{
		return $this->canvas;
	}

	public static function mmToPx( $length )
	{
		// TODO get from conf. Take into account
		// the fact that output DPI != input DPI !!!!
		$dpi = 100;
		$lengthPx = $length * $dpi / 25.4;
		return (int) round( $lengthPx );
	}

}
